Opt("MustDeclareVars",1)
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
;#include <WinAPI.au3>

#include "D:\Code\Repositories\dvd-assistant\OtherLibraries\FileCopyWithProgress.au3"
;_CopyWithProgress($sFrom, $sTo)
;returns true or false, depending on copy status

Global Const $regKeyHome = "HKEY_CURRENT_USER\Software\Nathan\DVDAssistant"


Global $lastUsedDrive = ""
Global $lastUsedDestinationFolder = ""

$lastUsedDrive = RegRead($regKeyHome , "drive")
$lastUsedDestinationFolder = RegRead($regKeyHome , "destination")



Dim $DVDCopierAssistantMainForm = GUICreate("DVD Copier Assistant", 275, 138)

GUICtrlCreateLabel("Movie Title:", 8, 8, 59, 17)
Dim $GUI_MovieTitleInput = GUICtrlCreateInput("", 112, 8, 153, 21)

GUICtrlCreateLabel("DVD Drive:", 8, 40, 58, 17)
Dim $GUI_DVDDriveNavButton = GUICtrlCreateButton($lastUsedDrive, 112, 32, 153, 25)

GUICtrlCreateLabel("Destination Folder:", 8, 72, 92, 17)
Dim $GUI_DestinationFolderNavButton = GUICtrlCreateButton($lastUsedDestinationFolder, 112, 64, 153, 25)

Dim $GUI_CopyButton = GUICtrlCreateButton("Copy", 9, 108, 111, 25)

GUISetState(@SW_SHOW, $DVDCopierAssistantMainForm)

Dim $nMsg

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			ExitFunc()
		Case $GUI_DVDDriveNavButton
			GUICtrlSetData($GUI_DVDDriveNavButton,	FileSelectFolder("Please choose your DVD Drive", "My Computer"))
			If StringRight(GUICtrlRead($GUI_DVDDriveNavButton),1) <> "\" Then
				GUICtrlSetData($GUI_DVDDriveNavButton,	GUICtrlRead($GUI_DVDDriveNavButton) & "\")
			EndIf
			$lastUsedDrive = GUICtrlRead($GUI_DVDDriveNavButton)
			RegWriteing()
		Case $GUI_DestinationFolderNavButton
			GUICtrlSetData($GUI_DestinationFolderNavButton,	FileSelectFolder("Please choose your destination folder", "My Computer"))
			If StringRight(GUICtrlRead($GUI_DestinationFolderNavButton),1) <> "\" Then
				GUICtrlSetData($GUI_DestinationFolderNavButton,	GUICtrlRead($GUI_DestinationFolderNavButton) & "\")
			EndIf
			$lastUsedDestinationFolder = GUICtrlRead($GUI_DestinationFolderNavButton)
			RegWriteing()
		Case $GUI_CopyButton
			Dim $inputValue = GUICtrlRead($GUI_MovieTitleInput)
			If $inputValue == "" Then
				MsgBox( 0,"You must enter a DVD title!", "Please enter a DVD title.")
			Else
				Dim $newDirectory = $lastUsedDestinationFolder & $inputValue
				If(StringRight($newDirectory,1) <> "\") Then
					$newDirectory = $newDirectory & "\"
				EndIf
				
				If FileExists($newDirectory) Then
					Switch MsgBox(4 + 16 + 256 + 8192,"Overwrite existing folder?", "This folder already exists. Delete and create anew?")
						Case 6 ; Yes
							DirRemove($newDirectory,1)
							;MsgBox(0,"RegistryKeys", $lastUsedDrive & "VIDEO_TS" & @CRLF & $newDirectory)
							Copy($lastUsedDrive & "VIDEO_TS", $newDirectory)
							
						Case 7 ; No
					EndSwitch
				Else
					;MsgBox(0,"RegistryKeys", $lastUsedDrive & "VIDEO_TS" & @CRLF & $newDirectory)
					Copy($lastUsedDrive & "VIDEO_TS", $newDirectory)
				EndIf
				
			EndIf
	EndSwitch
WEnd

Func Copy($from, $to)
	DirCreate($to)
	;MsgBox(0,"DEBUG", @comspec & ' /c xcopy /E/I "' & $from & '" "' & $to & '"')
	;runwait(@comspec & ' /c xcopy /E/I "' & $from & '" "' & $to & '"')
	_CopyWithProgress($from, $to)
EndFunc

Func RegWriteing()
	RegWrite($regKeyHome, "drive", "REG_SZ", $lastUsedDrive)
	RegWrite($regKeyHome, "destination", "REG_SZ", $lastUsedDestinationFolder)
EndFunc

Func ExitFunc()
	RegWriteing()
	Exit
EndFunc

